package carol.pvp.producer.java.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import wangy.pvp.basiclibrary.basicType.AssIndicator;
import wangy.pvp.basiclibrary.basicType.AssState;
import wangy.pvp.basiclibrary.basicType.PostAssertion;
import wangy.pvp.basiclibrary.basicType.PreAssertion;

import carol.pvp.producer.basicLanguage.JavaLanguage;
import carol.pvp.producer.impl.LanguageProducerImpl;

public class JavaLanguageProducerImpl extends LanguageProducerImpl{
	
	protected ArrayList<String> producePreCode(Iterator<PostAssertion> postAss){
		ArrayList<String> preCode = new ArrayList<String>();
		preCode.add(JavaLanguage.JAVA_PRE_DEC);
		
		while(postAss.hasNext()){
			PostAssertion ass = postAss.next();
			HashMap<PreAssertion,String> preRelation = ass.getPre();
			Set<PreAssertion> preSet = preRelation.keySet();
			Iterator<PreAssertion> preList = preSet.iterator();
			
			String ifCode = JavaLanguage.JAVA_IF_START;
			while(preList.hasNext()){
				ifCode = ifCode+JavaLanguage.JAVA_LEFT_HALF;
				PreAssertion preAss = preList.next();
				Iterator<AssState> stateList = preAss.getState();
				while(stateList.hasNext()){
					AssState state = stateList.next();
					String[] status = state.getStatus();
					ifCode = ifCode+state.getName()+status[0]+status[1];
					if(stateList.hasNext()) ifCode = ifCode+JavaLanguage.JAVA_AND_RELATION;
				}
				ifCode = ifCode+JavaLanguage.JAVA_RIGHT_HALF;
				if(preList.hasNext()) ifCode = ifCode+preRelation.get(preAss);
			}
			ifCode = ifCode+JavaLanguage.JAVA_IF_END;
			preCode.add(ifCode);
			String bodyCode = JavaLanguage.JAVA_PRE_IF_BODY_START+ass.getName()+JavaLanguage.JAVA_PRE_IF_BODY_END;
			preCode.add(bodyCode);
		}
		
		preCode.add(JavaLanguage.JAVA_PRE_BODY);
		preCode.add(JavaLanguage.JAVA_PRE_ERROR_HANDLER);
		return preCode;
	}
	
	protected ArrayList<String> producePostCode(Iterator<PostAssertion> postAss){
		ArrayList<String> postCode = new ArrayList<String>();
		
		while(postAss.hasNext()){
			PostAssertion ass = postAss.next();
			
			String checkCode = JavaLanguage.JAVA_POST_CHECK_START+ass.getName()+JavaLanguage.JAVA_POST_CHECK_END;
			postCode.add(checkCode);
			postCode.add(JavaLanguage.JAVA_LEFT_BRACE);
			
			String ifCode = JavaLanguage.JAVA_IF_START+JavaLanguage.JAVA_NOT_RELATION+JavaLanguage.JAVA_LEFT_HALF;
			Iterator<AssState> stateList = ass.getState();
			while(stateList.hasNext()){
				AssState state = stateList.next();
				String[] status = state.getStatus();
				ifCode = ifCode+state.getName()+status[0]+status[1];
				if(stateList.hasNext()) ifCode = ifCode+JavaLanguage.JAVA_AND_RELATION;
			}
			ifCode = ifCode+JavaLanguage.JAVA_RIGHT_HALF;
			
			Iterator<AssIndicator> indicatorList = ass.getIndicators();
			if(indicatorList.hasNext()){ifCode = ifCode+JavaLanguage.JAVA_AND_RELATION;}
			ifCode = ifCode+JavaLanguage.JAVA_LEFT_HALF;
			while(indicatorList.hasNext()){
				AssIndicator indicator = indicatorList.next();
				ifCode = ifCode+indicator.getName()+JavaLanguage.JAVA_EQUAL_RELATION+indicator.getValue();
				if(indicatorList.hasNext()){ifCode = ifCode+JavaLanguage.JAVA_AND_RELATION;}
			}
			ifCode = ifCode+JavaLanguage.JAVA_RIGHT_HALF;
			
			ifCode = ifCode+JavaLanguage.JAVA_IF_END;
			postCode.add(ifCode);
			
			postCode.add(JavaLanguage.JAVA_POST_ERROR_HANDLER);
			postCode.add(JavaLanguage.JAVA_RIGHT_BRACE);
		}
		
		return postCode;
	}	
}

